﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using System.Net.Http;
using System.Xml.Serialization;
using System.Xml;
using System.Net;

namespace ParseConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            get();
            Console.ReadKey();
        }

        static readonly int[] keys = { 604825, 607300, 613847, 615318, 624009, 631856, 635451, 637218, 640529, 643036, 652687, 658008, 662481, 669598, 675545, 685034, 687703, 696444, 702593, 703894, 711191, 714166, 720579, 728970, 738675, 740918, 743009, 747240, 750347, 759846, 764051, 770064, 773457, 779858, 786843, 790526, 799973, 803260, 808441, 816028, 825381, 827516, 832463, 837868, 843091, 852548, 858315, 867580, 875771, 879698, 882759, 885564, 888837, 896168 };

        static async void get()
        {
            HttpClient client = new HttpClient();
            string str = await client.GetStringAsync("https://raw.githubusercontent.com/kcwikizh/poi-plugin-subtitle/master/subtitles.json");

            JObject obj = JObject.Parse(str);
            SubTitles.Model.Data data = new SubTitles.Model.Data();
            foreach (JProperty i in obj.Children())
            {
                int ship = Convert.ToInt32(i.Name);
                foreach (JProperty j in i.Value)
                {
                    data.VoiceList.Add(new SubTitles.Model.Voice
                    {
                        ShipIndex=ship,
                        VoiceIndex = Convert.ToInt32(j.Name),
                        Translation = j.Value.Value<string>()
                    });
                }
            }
            data.VoiceKeys = keys.ToList();

            XmlWriterSettings setting = new XmlWriterSettings();
            setting.Indent = false;
            setting.NewLineHandling = NewLineHandling.None;
            using (FileStream fs = new FileStream("data.xml", FileMode.Create))
            using (XmlWriter writer = XmlWriter.Create(fs, setting))
            {
                XmlSerializer se = new XmlSerializer(typeof(SubTitles.Model.Data));
                se.Serialize(writer, data);
            }

            Console.WriteLine("Done");
        }
    }
}
